package com.example.lin.day9_26;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.example.lin.day9_26.pojo.ResultData;
import com.example.lin.day9_26.pojo.User;

import java.io.File;
import java.util.ArrayList;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }


    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        test3();
    }

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://192.168.3.100:8080/home/")
            //指定转换流
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    public static final MediaType TYPE_FILE = MediaType.parse("application/octet-stream");

    private void test3() {
        IUserRequest iUserRequest = retrofit.create(IUserRequest.class);//动态代理

        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "timg.jpg");
        final RequestBody requestBody = RequestBody.create(TYPE_FILE, file);


        MultipartBody.Part formData1 = MultipartBody.Part.createFormData("f1", "aaa" + file.getName(), requestBody);

        MultipartBody.Part formData2 = MultipartBody.Part.createFormData("f2", "bbb" + file.getName(), requestBody);

        ArrayList<MultipartBody.Part> parts = new ArrayList<>();
        parts.add(formData1);
        parts.add(formData2);


        Call<ResultData> dataCall = iUserRequest.uploadIcon("测试,跟随图片一起提交的键值对", parts);


        dataCall.enqueue(new Callback<ResultData>() {
            @Override
            public void onResponse(Call<ResultData> call, Response<ResultData> response) {
                Toast.makeText(MainActivity.this, response.body().toString(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(Call<ResultData> call, Throwable t) {

            }
        });
    }


    private void test2() {


        IUserRequest iUserRequest = retrofit.create(IUserRequest.class);//动态代理
        Call<ResultData<User>> getUser = iUserRequest.getUser("随便");
        getUser.enqueue(new Callback<ResultData<User>>() {
            @Override
            public void onResponse(Call<ResultData<User>> call, Response<ResultData<User>> response) {
                Toast.makeText(MainActivity.this, response.body().toString(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(Call<ResultData<User>> call, Throwable t) {

            }
        });


    }

    /**
     * Retrofit 实现POST请求 , 注册操作
     */
    private void test1() {
        IUserRequest iUserRequest = retrofit.create(IUserRequest.class);//动态代理

        Call<ResultData> call = iUserRequest.register("abc123", "123456");

        call.enqueue(new Callback<ResultData>() {
            @Override
            public void onResponse(Call<ResultData> call, Response<ResultData> response) {
                ResultData body = response.body();
                Toast.makeText(MainActivity.this, body.toString(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(Call<ResultData> call, Throwable t) {
                Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
